﻿using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;


public interface IImageGenerator
{
    void Generate();
}

public class TaxImageGenerator : IImageGenerator
{
    private string _DestinationPath;
    private TaxThemeInformation _ThemeInfo;

    public TaxImageGenerator(TaxThemeInformation themeInfo, string destinationPath)
    {
        this._ThemeInfo = themeInfo;
        this._DestinationPath = destinationPath;
    }

    public void Generate()
    {       
        this.GenerateButton();
        GenerateRightButton();
        this.GenerateButtonLogin();
        this.GenerateButtonFilter();
        this.GenerateFilterArrow();

        this.GeneratePageHeader();
        this.GenerateClientBox();
        this.GenerateGridImages();
        this.GenerateDropDown();
        this.GenerateSprite();
    }

    protected void GenerateButton()
    {
        Color mainColor = ColorTranslator.FromHtml(this._ThemeInfo.ButtonColor);
        Color subColor = ColorTranslator.FromHtml(this._ThemeInfo.ButtonSecondaryColor);

        Color mainRedColor = ColorTranslator.FromHtml("#A014D0");
        Color subRedColor = ColorTranslator.FromHtml("#CF251D");


        string fullPath = Path.Combine(this._DestinationPath, "Images\\btn_l_36.png");
        TaxRectangleStyle buttonStyle = new TaxRectangleStyle 
        {
            Width = 432,
            Height = 36,
            MainColor = mainColor, 
            SubColor = subColor,
            HasTopRight=false,
            HasBottomRight=false,
            Radius = 18
           
        };

        Bitmap btn36Org = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);
        buttonStyle.Alpha = 150;
        Bitmap btn36Opacity = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);

        buttonStyle.Radius = 14;
        buttonStyle.Height = 28;
        buttonStyle.Alpha = 255;
        Bitmap btn27Org = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);
        buttonStyle.Alpha = 150;
        Bitmap btn27Opacity = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);

        buttonStyle.Radius = 18;
        buttonStyle.Height = 36;
        buttonStyle.Alpha = 255;
        buttonStyle.MainColor = mainRedColor;
        buttonStyle.SubColor = subRedColor;

        Bitmap btnRed36Org = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);
        buttonStyle.Alpha = 150;
        Bitmap btnRed36Opacity = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);

        buttonStyle.Radius = 14;
        buttonStyle.Height = 28;
        buttonStyle.Alpha = 255;
        Bitmap btnRed27Org = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);
        buttonStyle.Alpha = 150;
        Bitmap btnRed27Opacity = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);


        Bitmap button = new Bitmap(432, 264);
        Graphics graphic = GraphicExtension.CreateGraphic(button);
        graphic.DrawImage((Image)btn36Org, new Point(0, 0));
        graphic.DrawImage((Image)btn36Opacity, new Point(0, 37));
        graphic.DrawImage((Image)btnRed36Org, new Point(0, 74));
        graphic.DrawImage((Image)btnRed36Opacity, new Point(0, 111));
        graphic.DrawImage((Image)btn27Org, new Point(0, 148));
        graphic.DrawImage((Image)btn27Opacity, new Point(0, 177));
        graphic.DrawImage((Image)btnRed27Org, new Point(0, 206));
        graphic.DrawImage((Image)btnRed27Opacity, new Point(0, 235));

        GraphicExtension.SaveToPngImage(fullPath, button, 100L);
    }

    protected void GenerateRightButton()
    {
        Color mainColor = ColorTranslator.FromHtml(this._ThemeInfo.ButtonColor);
        Color subColor = ColorTranslator.FromHtml(this._ThemeInfo.ButtonSecondaryColor);

        Color mainRedColor = ColorTranslator.FromHtml("#A014D0");
        Color subRedColor = ColorTranslator.FromHtml("#CF251D");


        string fullPath = Path.Combine(this._DestinationPath, "Images\\btn_r_36.png");
        TaxRectangleStyle buttonStyle = new TaxRectangleStyle
        {
            Width = 21,
            Height = 36,
            MainColor = mainColor,
            SubColor = subColor,
            HasTopLeft=false,
            HasBottomLeft=false,
            Radius = 18

        };

        Bitmap btn36Org = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);
        buttonStyle.Alpha = 150;
        Bitmap btn36Opacity = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);

        buttonStyle.Radius = 14;
        buttonStyle.Height = 28;
        buttonStyle.Alpha = 255;
        Bitmap btn27Org = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);
        buttonStyle.Alpha = 150;
        Bitmap btn27Opacity = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);

        buttonStyle.Radius = 18;
        buttonStyle.Height = 36;
        buttonStyle.Alpha = 255;
        buttonStyle.MainColor = mainRedColor;
        buttonStyle.SubColor = subRedColor;

        Bitmap btnRed36Org = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);
        buttonStyle.Alpha = 150;
        Bitmap btnRed36Opacity = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);

        buttonStyle.Radius = 14;
        buttonStyle.Height = 28;
        buttonStyle.Alpha = 255;
        Bitmap btnRed27Org = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);
        buttonStyle.Alpha = 150;
        Bitmap btnRed27Opacity = GraphicExtension.GenerateGradientRect(buttonStyle, string.Empty);


        Bitmap button = new Bitmap(21, 264);
        Graphics graphic = GraphicExtension.CreateGraphic(button);
        graphic.DrawImage((Image)btn36Org, new Point(0, 0));
        graphic.DrawImage((Image)btn36Opacity, new Point(0, 37));
        graphic.DrawImage((Image)btnRed36Org, new Point(0, 74));
        graphic.DrawImage((Image)btnRed36Opacity, new Point(0, 111));
        graphic.DrawImage((Image)btn27Org, new Point(0, 148));
        graphic.DrawImage((Image)btn27Opacity, new Point(0, 177));
        graphic.DrawImage((Image)btnRed27Org, new Point(0, 206));
        graphic.DrawImage((Image)btnRed27Opacity, new Point(0, 235));

        GraphicExtension.SaveToPngImage(fullPath, button, 100L);
    }
    protected void GenerateButtonLogin()
    {
        Color mainColor = ColorTranslator.FromHtml(this._ThemeInfo.ButtonColor);
        Color subColor = ColorTranslator.FromHtml(this._ThemeInfo.ButtonSecondaryColor);
        string fullPath = string.Empty;
        TaxRectangleStyle loginStyle = new TaxRectangleStyle { Width = 21, Height = 36, MainColor = mainColor, SubColor = subColor, Radius = 18 };

        loginStyle.HasTopRight = false;
        loginStyle.HasBottomRight = false;
        loginStyle.HasTopLeft = true;
        loginStyle.HasBottomLeft = true;
        fullPath = Path.Combine(this._DestinationPath, "Images\\btn_l.png");
        GraphicExtension.GenerateGradientRect(loginStyle, fullPath);

        loginStyle.Width = 432;
        loginStyle.HasTopRight = true;
        loginStyle.HasBottomRight = true;
        loginStyle.HasTopLeft = false;
        loginStyle.HasBottomLeft = false;
        fullPath = Path.Combine(this._DestinationPath, "Images\\btn_r.png");
        GraphicExtension.GenerateGradientRect(loginStyle, fullPath);
    }
    protected void GenerateButtonFilter()
    {
        Color mainColor = ColorTranslator.FromHtml(this._ThemeInfo.ButtonColor);
        Color subColor = ColorTranslator.FromHtml(this._ThemeInfo.ButtonSecondaryColor);
        TaxRectangleStyle slideStyle = new TaxRectangleStyle
        {
            Width = 154,
            Height = 41,
            MainColor = mainColor,
            SubColor = subColor,
            Radius = 15,
            HasTopLeft = false,
            HasTopRight = false
        };

        string fullPath = Path.Combine(this._DestinationPath, "Images\\btn-slide.png");
        GraphicExtension.GenerateGradientRect(slideStyle, fullPath);
    }
    protected void GenerateFilterArrow()
    {
        Color mainColor = ColorTranslator.FromHtml(this._ThemeInfo.ButtonTextColor);
        Bitmap arrow = new Bitmap(24, 70);
        arrow.DrawAngleTriangle(0, 5, 10, mainColor);
        arrow.DrawRectangle(0, 12, 10, 1, mainColor);

        arrow.DrawRectangle(0, 61, 10, 1, mainColor);
        arrow.DrawTriangle(0, 64, 10, mainColor);

        string fullPath = Path.Combine(this._DestinationPath, "Images\\white-arrow.png");
        GraphicExtension.SaveToPngImage(fullPath, arrow, 100L);
    }

    protected void GeneratePageHeader()
    {
        Color rightColor = ColorTranslator.FromHtml(this._ThemeInfo.RightHeaderBackgroundColor);
        Color leftColor = ColorTranslator.FromHtml(this._ThemeInfo.LeftHeaderBackgroundColor);
        Bitmap pageHeader = new Bitmap(2500, 70);
        pageHeader.FillRectangle(0, -2, 960, 74, leftColor);
        pageHeader.FillRectangle(960, -2, 1540, 74, rightColor);  
        string fullPath = Path.Combine(this._DestinationPath, "Images\\shade.png");
        GraphicExtension.SaveToPngImage(fullPath, pageHeader, 100L);
    }
    protected void GenerateClientBox()
    {
        Color mainColor = ColorTranslator.FromHtml(this._ThemeInfo.LoginAddrBackgroundColor);
        Color subColor = ColorTranslator.FromHtml(this._ThemeInfo.LoginAddrBackgroundColor);
        string fullPath = string.Empty;
        TaxRectangleStyle clientStyle = new TaxRectangleStyle
        {
            Radius = 10,
            Width = 335, Height = 13,
            MainColor = mainColor, SubColor = subColor,
            HasTopRight = false, HasBottomRight = false,  HasTopLeft = true,  HasBottomLeft = false
        };
        fullPath = Path.Combine(this._DestinationPath, "Images\\box_grey_t.png");
        GraphicExtension.GenerateGradientRect(clientStyle, fullPath);

        clientStyle.HasTopLeft = false;
        clientStyle.HasBottomLeft = true;
        fullPath = Path.Combine(this._DestinationPath, "Images\\box_grey_b.png");
        GraphicExtension.GenerateGradientRect(clientStyle, fullPath);
    }
    protected void GenerateGridImages()
    {
        Color mainColor = ColorTranslator.FromHtml(this._ThemeInfo.PrimaryColor);        
        string fullPath = string.Empty;        

        Bitmap gridHeader = new Bitmap(70, 50);
        gridHeader.FillRectangle(-1, -1, 72, 52, mainColor);
        fullPath = Path.Combine(this._DestinationPath, "Images\\header-grid.jpg");
        GraphicExtension.SaveToJpgImage(fullPath, gridHeader, 100L);

        
        Bitmap gridFooterLeft = new Bitmap(28,28);
        gridFooterLeft.FillRectangle(-1, -1, 30, 30, mainColor);
        fullPath = Path.Combine(this._DestinationPath, "Images\\menutableft.jpg");
        GraphicExtension.SaveToJpgImage(fullPath, gridFooterLeft, 100L);

        Bitmap gridFooterRight = new Bitmap(28, 28);
        gridFooterRight.FillRectangle(-1, -1, 30, 30, mainColor);
        fullPath = Path.Combine(this._DestinationPath, "Images\\menutabright.jpg");
        GraphicExtension.SaveToJpgImage(fullPath, gridFooterRight, 100L);

    }
    protected void GenerateDropDown()
    {
        Color mainColor = ColorTranslator.FromHtml(this._ThemeInfo.ButtonColor);
        Color subColor = ColorTranslator.FromHtml(this._ThemeInfo.ButtonSecondaryColor);
        Color textColor = ColorTranslator.FromHtml(this._ThemeInfo.ButtonTextColor);

        TaxRectangleStyle comboStyle = new TaxRectangleStyle
        {
            Radius = 0,
            Width = 28,
            Height = 31,
            MainColor = mainColor,
            SubColor = subColor,
            HasTopRight = false,
            HasBottomRight = false,
            HasTopLeft = false,
            HasBottomLeft = false,
        };
        string fullPath = Path.Combine(this._DestinationPath, "Images\\combo-box.jpg");
        Bitmap rect = GraphicExtension.GenerateGradientRect(comboStyle, string.Empty);
        rect.DrawTriangle(10, 13, 7, textColor);
        GraphicExtension.SaveToJpgImage(fullPath, rect, 100L);

    }

    protected void GenerateSprite()
    {
        string spritePath = Path.Combine(this._DestinationPath, "Images\\sprite.gif");       
        Image img = GraphicExtension.ReadFromFile(spritePath);
        Bitmap bitmap = new Bitmap(img);
        Graphics graphics = GraphicExtension.CreateGraphicNotClear(bitmap);
        SolidBrush pen = new SolidBrush(ColorTranslator.FromHtml(this._ThemeInfo.GridHeaderTextColor));
        graphics.FillPolygon(pen, new Point[] { new Point(0, 1256), new Point(5, 1250), new Point(5, 1261) }, FillMode.Alternate);
        graphics.FillPolygon(pen, new Point[] { new Point(26, 1250), new Point(26, 1261), new Point(31, 1256) }, FillMode.Winding);
       
        GraphicExtension.SaveToPngImage(spritePath, bitmap, 100L);
    }
}
